#include "client.h"

void handle_cd(ClientInfo* client, int argc, char* argv[]) {
    if (argc != 2) {
        printf("用法: cd <目录>\n");
        return;
    }

    Message msg={0};
    msg.type = MSG_TYPE_CD;
    msg.length = snprintf(msg.data, sizeof(msg.data), "%s", argv[1]);
    // printf("%s\n", argv[1]);

    if (send_message(client->socket, &msg) < 0) {
        perror("发送cd命令失败");
        return;
    }

    if (receive_message(client->socket, &msg) < 0) {
        perror("接收cd命令响应失败");
        return;
    }

    if (msg.type == MSG_TYPE_SUCCESS) {
        // 更新客户端的current_dir
        strncpy(client->current_dir, msg.data, PATH_MAX);
        printf("成功切换到目录: %s\n", client->current_dir);
    } else {
        printf("切换目录失败: %s\n", msg.data);
    }
}